package com.wavercaz.freeforum.controllers;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wavercaz.freeforum.pojo.CommonSearchParam;
import com.wavercaz.freeforum.pojo.Report;
import com.wavercaz.freeforum.pojo.vo.ReportVo;
import com.wavercaz.freeforum.services.ReportService;
import com.wavercaz.freeforum.utils.JWTUtil;
import com.wavercaz.freeforum.utils.QueryUtil;
import com.wavercaz.freeforum.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class ReportController {
    @Autowired
    private  ReportService reportService;

    // 增
    @PutMapping("/report")
    public ResponseResult save(@RequestHeader("Authorization") String token, @RequestBody Report report) {
        System.out.println(report);
        report.setUserId(Integer.parseInt(JWTUtil.verifyToken(token).getClaim("id").asString()));
        report.setStatus(1);
        boolean result = reportService.save(report);
        if(!result) return new ResponseResult(HttpStatus.NO_CONTENT.value(), "举报失败");
        else return new ResponseResult(HttpStatus.OK.value(), "举报成功");
    }

    // 删
    @DeleteMapping("/report")
    public ResponseResult delete(@RequestBody List<Integer> ids) {
        boolean result = reportService.removeByIds(ids);
        if(!result) return new ResponseResult(HttpStatus.NO_CONTENT.value(), "删除失败");
        else return new ResponseResult(HttpStatus.OK.value(), "删除成功");
    }

    // 改
    @PostMapping("/report")
    public ResponseResult update(@RequestBody Report report) {
        boolean result = reportService.updateById(report);
        if(!result) return new ResponseResult(HttpStatus.NO_CONTENT.value(), "修改失败");
        else return new ResponseResult(HttpStatus.OK.value(), "修改成功");
    }

    // 查
    @GetMapping("/report/{id}")
    public ResponseResult get(@PathVariable Integer id ) {

        Report report = reportService.getById(id);
        if(report == null) return new ResponseResult(HttpStatus.NO_CONTENT.value(), "无此相关信息");
        else return new ResponseResult(HttpStatus.OK.value(), report);
    }

    // 列表
    @PostMapping("/reportList")
    public ResponseResult list(@RequestBody(required = false) CommonSearchParam commonSearchParam) {
        Object data = null;
        if(commonSearchParam != null) {
            QueryWrapper queryWrapper = QueryUtil.getQueryWrapper(commonSearchParam.getSearches());
            if(commonSearchParam.getPageNum()!=null && commonSearchParam.getPageSize()!=null)
                data= reportService.page(new Page<>(commonSearchParam.getPageNum(), commonSearchParam.getPageSize()), queryWrapper);
            else data= reportService.list(queryWrapper);
        }
        else data= reportService.list();
        return new ResponseResult(HttpStatus.OK.value(), data);
    }

    // Vo列表
    @PostMapping("/reportVoList")
    public ResponseResult listVo(@RequestBody(required = false) CommonSearchParam commonSearchParam) {
        IPage<ReportVo> data = reportService.getReportVoList(commonSearchParam, new Page<>(commonSearchParam.getPageNum(), commonSearchParam.getPageSize()));
        return new ResponseResult(HttpStatus.OK.value(), data);
    }
}
